function Table(id, url, type, pageSize) {
    this.id = id;
    this.url = url;
    this.page = 1;
    this.pageSize = pageSize || 10;
    this.data = [];
    this.type = type || 0; //0 代表收信箱，1 代表草稿箱，2 代表已发送
}

Table.prototype = {

    create: function () {
        $('#pageSize').val(this.pageSize);
        var _this = this;
        var url;
        if (_this.url.indexOf('?') == -1) {
            url = _this.url + '?page=' + _this.page + '&pageSize=' + _this.pageSize;
        } else {
            url = _this.url + '&page=' + _this.page + '&pageSize=' + _this.pageSize;
        }
        console.log(url);
        getAjax(url, function (data) {
            if (data) {
                _this.data = data;
                var html;
                for (var i = 0; i < _this.data.length; i++) {
                    let item = _this.data[i];
                    let tr = '<tr data-id="'+ item.title + '"><td><input data="' + item.title + '" name="check" type="checkbox"/></td>';
                    if(_this.type == 0){
                        if (item.isRead == '0') {
                            tr += '<td><span class="fa fa-envelope-open-o fa-lg fa-color"></span></td>';
                        } else {
                            tr += '<td><span class="fa fa-envelope fa-lg color-unread"></span></td>';
                        }
                    }
                    if(_this.type == 1){
                        tr += '<td><span class="fa fa-envelope-o fa-lg back-fa fa-color"><span class="fa fa-pencil back-top-fa"></span></span></td>';
                    }
                    if(_this.type == 2){
                        tr += '<td><span class="fa fa-envelope-o fa-lg back-fa fa-color"><span class="fa fa-check-circle back-top-fa" style="color: #0aef0a;font-size: 14px;left: 12px;"></span></span></td>';

                        //tr += '<td><span class="fa fa-envelope-o fa-lg back-fa fa-color"><span class="fa fa-check-circle back-top-fa" style="color: #ef1919;font-size: 14px;left: 12px;"></span></span></td>';

                    }
                    tr += '<td>' + item.user + '</td><td>' + item.title + '</td><td>' + item.time + '</td>';
                    if (i == 0) {
                        html = tr;
                    } else {
                        html += tr;
                    }
                }
                $('#' + _this.id).html(html);
                let items = $('#' + _this.id + ' tr');
                for (let j = 0; j < items.length; j++) {
                    var td = items.eq(j).find('td');
                    for (let z = 1; z < td.length; z++) {
                        td.eq(z).click(function (z) {
                            var id = $(this).parent().attr("data-id");
                            sessionStorage.setItem('mailId', id);
                            if(_this.type == 0) sessionStorage.setItem('type',0);
                            if(_this.type == 2) sessionStorage.setItem('type', 2);
                            if(_this.type == 1){
                                window.location.href = 'write.html'; //根据id读取数据。
                            }else{
                                window.location.href = 'viewMail.html'; //根据id读取数据。
                            }

                        })
                    }
                }
                _this.check();
            }
        })
    },
    delData: function (url) {
        var _this = this;
        getAjax(url, function (data) {
            if (data) {
                if (data.success == true) {
                    alert(data.msg);
                    _this.create();
                } else {
                    alert(data.msg)
                }
            } else {
                console.log('没有返回消息')
            }
        })
    },
    setPage: function (num) {
        this.page = parseInt(num);
        this.create();
    },
    setPageSize: function (num) {
        this.pageSize = parseInt(num);
        this.create();
    },
    check: function () {
        var _this = this;
        $('input[name="all"]').click(function () {
            if (this.checked) {
                $("input[name='check']:checkbox").each(function () { //遍历所有的name为selectFlag的 checkbox
                    $(this).prop("checked", true);
                })
            } else {
                $("input[name='check']:checkbox").each(function () { //遍历所有的name为selectFlag的 checkbox
                    $(this).prop("checked", false);
                })
            }
        });
        var checkAll = false;
        $("input[name='check']:checkbox").each(function () {
            $(this).click(function () {
                $("input[name='check']:checkbox").each(function () {
                    if (this.checked) {
                        checkAll = true;
                    } else {
                        checkAll = false;
                        return false;
                    }
                });
                if (checkAll) {
                    $('input[name="all"]').prop("checked", true);
                } else {
                    $('input[name="all"]').prop("checked", false);
                }
            })
        })
    },
    getCheckVal: function () {
        var _this = this;
        var item = [];
        $("input[name='check']:checkbox").each(function (index) {
            if (this.checked) {
                item.push(_this.data[index]);
            }
        });
        return item;
    },
    getCheckId: function () {
        var ids = [];
        $("input[name='check']:checkbox").each(function (index) {
            if (this.checked) {
                ids.push($(this).attr("data"));
            }
        });
        return ids;
    },
    getIds: function () {
        console.log(this.data);
        var ids = [];
        for (var i = 0; i < this.data.length; i++) {
            ids.push(this.data[i].title);
        }
        return ids;
    }
};

